<?php

namespace App\Domain\ChannelContact\Dao;

use App\Domain\ChannelContact\Models\ChannelContactModel;
use App\Http\Models\Permission\PermissionModel;
use Illuminate\Database\Eloquent\Builder;

class ChannelContactDao
{
    public function findByName($name)
    {
        return ChannelContactModel::where('name', $name)->where('state', 1)->first();
    }

    public function findNew()
    {
        return ChannelContactModel::orderby('id','desc')->first();
    }

    public function findById($id)
    {
        return ChannelContactModel::where('id', $id)->where('state', 1)->first();
    }

    public function findByContactId($id)
    {
        return ChannelContactModel::where('contact_id', $id)->where('state', 1)->first();
    }

    public function del($id)
    {
        $ids = explode(',',$id);
        return ChannelContactModel::whereIn('id', $ids)->update(['state' => '0']);
    }

    /**
     * 创建合同
     * @param $params
     * @return ChannelContactModel
     */
    public function create($params)
    {
        $params = array_filter_null($params);
        return ChannelContactModel::create($params);
    }

    /**
     * 修改合同
     * @param ChannelContactModel $model
     * @param $param
     * @return ChannelContactModel
     */
    public function update(ChannelContactModel $model, $param)
    {
        $param = array_filter_null($param);
        $model->fill($param)->save();
        return $model;
    }
}
